package cn.iocoder.yudao.module.etl.controller.admin.datasource;

import cn.iocoder.yudao.module.etl.request.datasource.UpdataDsRequest;
import cn.iocoder.yudao.module.etl.dal.dataobject.Datasource;
import cn.iocoder.yudao.module.etl.dto.DatasourceDTO;
import cn.iocoder.yudao.module.etl.dto.datasource.DBTableDTO;
import cn.iocoder.yudao.module.etl.request.datasource.ApiDefinition;
import cn.iocoder.yudao.module.etl.request.datasource.DatasourceUnionRequest;
import cn.iocoder.yudao.module.etl.service.datasource.DatasourceService;
import cn.iocoder.yudao.module.etl.utils.ResultHolder;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "数据源：数据源管理")
@ApiSupport(order = 30)
@RequestMapping("etl/datasource")
@RestController
public class DatasourceController {

    @Resource
    private DatasourceService datasourceService;

    @ApiOperation("新增数据源")
    @PostMapping("/add")
    public Datasource addDatasource(@RequestBody Datasource datasource) throws Exception{
        return datasourceService.addDatasource(datasource);
    }

    @ApiOperation("数据源类型")
    @GetMapping("/types")
    public ResultHolder types() throws Exception{
        return ResultHolder.success(datasourceService.types());
    }

    @ApiIgnore
    @PostMapping("/validate")
    public ResultHolder validate(@RequestBody Datasource datasource) throws Exception {
        return datasourceService.validate(datasource) ;
    }

    @ApiOperation("验证数据源")
    @GetMapping("/validate/{datasourceId}")
    public ResultHolder validate(@PathVariable String datasourceId) {
        return datasourceService.validate(datasourceId);
    }

    @ApiOperation("查询当前用户数据源")
    @GetMapping("/list")
    public  ResultHolder  getDatasourceList() throws Exception {
        DatasourceUnionRequest request = new DatasourceUnionRequest();
//        request.setUserId(String.valueOf(AuthUtils.getUser().getUserId()));
        List<DatasourceDTO> datasourceList = datasourceService.getDatasourceList(request);
        return ResultHolder.success(datasourceList);
    }

    @ApiOperation("查询当前用户数据源")
    @GetMapping("/list/{type}")
    public ResultHolder getDatasourceListByType(@PathVariable String type) throws Exception {
        DatasourceUnionRequest request = new DatasourceUnionRequest();
        List<DatasourceDTO> datasourceList = datasourceService.getDatasourceList(request);
        List<DatasourceDTO> collect = datasourceList.stream().filter(datasourceDTO -> datasourceDTO.getType().equalsIgnoreCase(type)).collect(Collectors.toList());
        return ResultHolder.success(collect);
    }
    @ApiOperation("删除数据源")
    @PostMapping("/delete/{datasourceID}")
    public ResultHolder deleteDatasource(@PathVariable(value = "datasourceID") String datasourceID) throws Exception {
        Datasource datasource = datasourceService.get(datasourceID);
//        SysLogDTO sysLogDTO = DeLogUtils.buildLog(SysLogConstants.OPERATE_TYPE.DELETE, SysLogConstants.SOURCE_TYPE.DATASOURCE, datasourceID, datasource.getType(), null, null);
        ResultHolder resultHolder = datasourceService.deleteDatasource(datasourceID);
//        if (ObjectUtils.isNotEmpty(resultHolder) && resultHolder.isSuccess())
//            DeLogUtils.save(sysLogDTO);
        return resultHolder;
    }

    @ApiOperation("更新数据源")
    @PostMapping("/update")
    public void updateDatasource(@RequestBody UpdataDsRequest dsRequest) throws Exception{
        datasourceService.updateDatasource(dsRequest);
    }

    @ApiOperation("查询数据源详情")
    @PostMapping("/get/{id}")
    public ResultHolder getDatasource(@PathVariable String id) throws Exception {
        Datasource dataSourceDetails = datasourceService.getDataSourceDetails(id);
        return ResultHolder.success(dataSourceDetails);
    }

    @ApiOperation("查询数据源下属所有表")
    @PostMapping("/getTables/{id}")
    public ResultHolder  getTables(@PathVariable String id) throws Exception {
        java.util.List<cn.iocoder.yudao.module.etl.dto.datasource.DBTableDTO> tables = datasourceService.getTables(id);
        return ResultHolder.success(tables);
    }

    @ApiIgnore
    @PostMapping("/getSchema")
    public List<String> getSchema(@RequestBody Datasource datasource) throws Exception {
       return datasourceService.getSchema(datasource);
    }

    @ApiIgnore
    @PostMapping("/checkApiDatasource")
    public ApiDefinition checkApiDatasource(@RequestBody ApiDefinition apiDefinition) throws Exception {
        return datasourceService.checkApiDatasource(apiDefinition);
    }


}
